package com.estate.struts.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.estate.business.TenderPublishService;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.TenderPublishVo;
import com.estate.vo.impl.TenderVo;

public class TenderPublishAction extends EstateSupport {

	  private String id;
		 private String tenderId;
		 private String title;
		 private String beginTime;
		 private String overTime;
		 private String regiPlace;
		 private String contactPeople;
		 private String contactPhone;
		 private String remark;
		 private String content;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private TenderPublishService tenderPublishService;
	
	private String directPage;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			TenderPublishVo tenderPublish = this.getAddTenderPublish();
			if(tenderPublish != null){
				boolean result = this.getTenderPublishService().addTenderPublish(tenderPublish);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> list = this.getTenderPublishService().getList();
			this.getRequest().setAttribute("list", list);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allList";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getTenderPublishService().getTenderPublish(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderPublish", vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getTenderPublishService().getTenderPublish(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderPublish", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			TenderPublishVo tenderPublish = this.getUpdateTenderPublish();
			if(tenderPublish == null){
				page = "fail";
			}else{
				boolean result = this.getTenderPublishService().updateTenderPublish(tenderPublish);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}
//		else if("post".equals(this.getMethod())){
//			this.getAnnouncementService().updatePost(Integer.parseInt(this.getId()));
//			page = this.SUCCESS;
//		}
		else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
//				System.out.println(this.getDelStr());
//				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getTenderPublishService().deleteMultiTenderPublish(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else if("enterAddPublish".equals(this.getMethod())){
				this.getRequest().setAttribute("tender", this.getTenderId());
				page = "addPublishPage";
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					TenderPublishVo tenderPublish = new TenderPublishVo();
					tenderPublish.setId(Integer.parseInt(this.getId()));
					boolean result = this.getTenderPublishService().deleteTenderPublish(tenderPublish);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}
	private TenderPublishVo getUpdateTenderPublish(){
		TenderPublishVo tenderPublish = new TenderPublishVo();
		
		int id = Integer.parseInt(this.getId());
		
		try {
			tenderPublish.setId(id);
			tenderPublish.setBeginTime(ProjectConstants.SIMPLE_DATE.parse(this.getBeginTime()));
			tenderPublish.setContactPeople(this.getContactPeople());
			tenderPublish.setContactPhone(this.getContactPhone());
			tenderPublish.setContent(this.getContent());
			tenderPublish.setOverTime(ProjectConstants.SIMPLE_DATE.parse(this.getOverTime()));
			tenderPublish.setRegiPlace(this.getRegiPlace());
			tenderPublish.setRemark(this.getRemark());
			tenderPublish.setTitle(this.getTitle());
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tenderPublish;
	}
	private TenderPublishVo getAddTenderPublish(){
		TenderPublishVo tenderPublish = new TenderPublishVo();
		
		try {
			tenderPublish.setBeginTime(ProjectConstants.SIMPLE_DATE.parse(this.getBeginTime()));
			tenderPublish.setContactPeople(this.getContactPeople());
			tenderPublish.setContactPhone(this.getContactPhone());
			tenderPublish.setContent(this.getContent());
			tenderPublish.setOverTime(ProjectConstants.SIMPLE_DATE.parse(this.getOverTime()));
			tenderPublish.setPostDate(new Date());
			tenderPublish.setPostUser(this.getSessionUser());
			tenderPublish.setRegiPlace(this.getRegiPlace());
			tenderPublish.setRemark(this.getRemark());
			tenderPublish.setTitle(this.getTitle());
			
			TenderVo tender = new TenderVo();
			tender.setId(Integer.parseInt(this.getTenderId()));
			tenderPublish.setTender(tender);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		return tenderPublish;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTenderId() {
		return tenderId;
	}
	public void setTenderId(String tenderId) {
		this.tenderId = tenderId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getBeginTime() {
		return beginTime;
	}
	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}
	public String getOverTime() {
		return overTime;
	}
	public void setOverTime(String overTime) {
		this.overTime = overTime;
	}
	public String getRegiPlace() {
		return regiPlace;
	}
	public void setRegiPlace(String regiPlace) {
		this.regiPlace = regiPlace;
	}
	public String getContactPeople() {
		return contactPeople;
	}
	public void setContactPeople(String contactPeople) {
		this.contactPeople = contactPeople;
	}
	public String getContactPhone() {
		return contactPhone;
	}
	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public TenderPublishService getTenderPublishService() {
		return tenderPublishService;
	}
	public void setTenderPublishService(TenderPublishService tenderPublishService) {
		this.tenderPublishService = tenderPublishService;
	}
	public String getDirectPage() {
		return directPage;
	}
	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
}
